package com.shxzz.easy12306.services.ticket.controller;

import com.shxzz.easy12306.framework.starter.convention.result.Result;
import com.shxzz.easy12306.framework.starter.convention.result.Results;
import com.shxzz.easy12306.framework.starter.idempotent.annotation.Idempotent;
import com.shxzz.easy12306.framework.starter.idempotent.enums.IdempotentKeyType;
import com.shxzz.easy12306.framework.starter.idempotent.enums.IdempotentScene;
import com.shxzz.easy12306.services.ticket.dto.req.CancelTicketOrderReqDTO;
import com.shxzz.easy12306.services.ticket.dto.req.PurchaseTicketReqDTO;
import com.shxzz.easy12306.services.ticket.dto.req.RefundTicketReqDTO;
import com.shxzz.easy12306.services.ticket.dto.req.TicketPageQueryReqDTO;
import com.shxzz.easy12306.services.ticket.dto.resp.PurchaseTicketRespDTO;
import com.shxzz.easy12306.services.ticket.dto.resp.TicketPageQueryRespDTO;
import com.shxzz.easy12306.services.ticket.service.TicketService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 列车车票控制层
 * @author shxzz
 * @create 2024-03-04-20:21
 */
@RequiredArgsConstructor
@RestController
public class TicketController {
    private final TicketService ticketService;

    /**
     * 查询车票
     */
    @GetMapping("/api/ticket-service/ticket/query")
    public Result<TicketPageQueryRespDTO> ticketSearch(TicketPageQueryReqDTO requestParam) {
        return Results.success(ticketService.search(requestParam));
    }

    /**
     * 购买车票
     */
    @Idempotent(
            scene = IdempotentScene.RESTAPI,
            type = IdempotentKeyType.SPEL,
            uniqueKeyPrefix = "ticket-service:lock:purchase-ticket:",
            spel = "T(com.shxzz.easy12306.framework.starter.user.core.UserContext).getUsername()",
            message = "正在出票，请稍后..."
    )
    @PostMapping("/api/ticket-service/ticket/purchase")
    public Result<PurchaseTicketRespDTO> purchaseTicket(@RequestBody PurchaseTicketReqDTO requestParam) {
        return Results.success(ticketService.purchaseTicket(requestParam));
    }

    /**
     * 购买车票 V2 版本
     */
    @Idempotent(
            scene = IdempotentScene.RESTAPI,
            type = IdempotentKeyType.SPEL,
            uniqueKeyPrefix = "ticket-service:lock:purchase-ticket:",
            spel = "T(com.shxzz.easy12306.framework.starter.user.core.UserContext).getUsername()",
            message = "正在出票，请稍后..."
    )
    @PostMapping("/api/ticket-service/ticket/purchase/v2")
    public Result<PurchaseTicketRespDTO> purchaseTicketsV2(@RequestBody PurchaseTicketReqDTO requestParam) {
        return Results.success(ticketService.purchaseTicketV2(requestParam));
    }


    /**
     * 取消车票订单
     */
    @PostMapping("/api/ticket-service/ticket/cancel")
    public Result<Void> cancelTicketOrder(@RequestBody CancelTicketOrderReqDTO requestParam) {
        ticketService.cancelTicketOrder(requestParam);
        return Results.success();
    }

    /**
     * 车票退款
     */
    @PostMapping("/api/ticket-service/ticket/refund")
    public Result<Void> commonTicketRefund(@RequestBody RefundTicketReqDTO requestParam) {
        return Results.success(ticketService.commonTicketRefund(requestParam));
    }
}
